/**
 * Created by zhou on 2016/11/25 0025.
 */
{
    angular.module('mainModule')
        .controller('DormitoryBuildingController', DormitoryBuildingController);
    DormitoryBuildingController.$inject = ['http.interfaces', '$stateParams', '$state',"$scope","$ionicLoading"];
    function DormitoryBuildingController(interfaces, $stateParams, $state,$scope,$ionicLoading) {
        let vm = this;
        let collect = {
            defaultData: defaultData,
            Changed: Changed,
            qhbtn: qhbtn,
            return_ssl: false,
            btnqh: false,
            info: {},
            studentList: false,
            studentInfo: false,
            fromEmptyBed:false,
            getFloor: getFloor,
            dormitoryBuildinginfo: dormitoryBuildinginfo,
            roomname: '',
            floorList: [],
            dormitoryList: [],
            floorNum: '',
            persons_length:[],
            dormitoryBuilding: '',
            floorRooms: {},
            List: [],
            selected: {},
            show_clear:show_clear,
            active: Active
        };
        collect.active();//进入控制器后调用一次
        vm.collect = collect;//输出集合，提供给模板调用
        vm.interfaces = interfaces;

        /** Active方法
         * @desc 控制器触发时执行一次的方法
         * @constructor
         */
        function Active() {
            $scope.$on('$ionicView.beforeEnter', function (event, data) {
                if ($state.current.name == 'tab.dormitoryBuilding2') {
                    collect.fromEmptyBed=true;
                }
                else collect.fromEmptyBed=false;
                dormitoryBuildinginfo();
                getFloor();
            });
        }

        function qhbtn(stuId) {
            if ($state.current.name == 'tab.dormitoryBuilding1') {
                $state.go("tab.studentInfo1", {id: stuId})
            }
            else if ($state.current.name == 'tab.dormitoryBuilding2') {
                $state.go("tab.studentInfo2", {id: stuId})
            }
        }

        function dormitoryBuildinginfo() {
            interfaces.dormitoryList({dormId: $stateParams.id}).then(res=> {
                if (res.data.resultCode == 1) {
                    collect.dormitoryList = res.data.data;
                    for (var i = 0; i < collect.dormitoryList.length; i++) {
                        if (collect.dormitoryList[i].dormId == $stateParams.id) {
                            collect.dormitoryBuilding = collect.dormitoryList[i].dormName;
                        }
                    }
                }
            });
        }

        function getFloor() {
            interfaces.floorList({dormId: $stateParams.id}).then(res=> {
                if (res.data.result == 1) {
                    collect.floorList = res.data.data;
                    collect.List.push({"departName": "全部", "departId": -1});
                    for (var i = 0; i < collect.floorList.length; i++) {
                        collect.List.push({"departName": collect.floorList[i] + '楼', "departId": collect.floorList[i]});
                    }
                    if (collect.List.length > 0) {
                        collect.selected = collect.List[0];
                    }
                    defaultData();
                }
            });
        }

        function defaultData(folid) {
            var params={};
            if(folid!=undefined){
                  params={dormId: $stateParams.id, floorNum: folid};
            }else{
                 params={dormId: $stateParams.id};
            }
            $ionicLoading.show();
            interfaces.floorRooms(params).then(res=> {
                $ionicLoading.hide();
                if (res.data.resultCode == 1) {
                    collect.floorRooms = res.data.data;
                }
            });
        }

        function Changed() {
            if(collect.selected.departId==-1){
                defaultData();
            }
            else{
                defaultData(collect.selected.departId);
            }

        }
        function show_clear(index,persons) {
            let bool=index%2;
            collect.persons_length.push(persons.length);
            console.log(collect.persons_length);
            if (bool==0){
                return true;
            }else {
                return false
            }

        }

    }
}